#include <mFS/FsTypes.h>
#include <mFS/mConfig.h>
#include <mHAL/hal/Hal.h>
#include <mFS/mFS.h>
#include <Utils/Mem.h>

#include "CommonAP.h"


static UINT16 CommonAP_MsgSM4DecryptWithPadding(UINT8 *pBuf, UINT16 nSize, UINT8 Key[])
{
	UINT16 nLen = 0;

	HalAlgSM4_Init(Key, NULL); 
	HalAlgSM4_Decrypt_Update(HALALGSM4_MODE_ECB, pBuf, nSize, pBuf); 
	HalAlgSM4_Final(); 

	nLen = CommonAP_AlgPaddingLength(pBuf, nSize);

	return (nLen);
}

static UINT16 CommonAP_MsgSSF33DecryptWithPadding(UINT8 *pBuf, UINT16 nSize, UINT8 Key[])
{
	UINT16 nLen = 0;

	HalAlgSSF33_Init(Key, NULL); 
	HalAlgSSF33_Decrypt_Update(HALALGSSF33_MODE_ECB, pBuf, nSize, pBuf); 
	HalAlgSSF33_Final(); 

	nLen = CommonAP_AlgPaddingLength(pBuf, nSize);

	return (nLen);
}

static UINT16 CommonAP_MsgSM1DecryptWithPadding(UINT8 *pBuf, UINT16 nSize, UINT8 Key[])
{
	UINT16 nLen = 0;

	HalAlgSM1_Init(Key, NULL); 
	HalAlgSM1_Decrypt_Update(HALALGSM1_MODE_ECB, pBuf, nSize, pBuf); 
	HalAlgSM1_Final(); 

	nLen = CommonAP_AlgPaddingLength(pBuf, nSize);

	return (nLen);
}

static UINT16 CommonAP_MsgDESDecryptWithPadding(UINT8 *pBuf, UINT16 nSize, UINT8 Key[])
{
	UINT16 nLen = 0;

	HalAlgDES_Init(Key, CommonAP_AlgGetKeyLen(HALALGDES1_MODE_ECB), NULL); 
	HalAlgDES_Decrypt_Update(HALALGDES1_MODE_ECB, pBuf, nSize, pBuf); 
	HalAlgDES_Final(); 

	nLen = CommonAP_AlgPaddingLength(pBuf, nSize);

	return (nLen);
}

static UINT16 CommonAP_MsgSM4Decrypt(UINT8 *pBuf, UINT16 nSize, UINT8 Key[])
{
	HalAlgSM4_Init(Key, NULL); 
	HalAlgSM4_Decrypt_Update(HALALGSM4_MODE_ECB, pBuf, nSize, pBuf); 
	HalAlgSM4_Final(); 

	return (0);
}

static UINT16 CommonAP_MsgSSF33Decrypt(UINT8 *pBuf, UINT16 nSize, UINT8 Key[])
{
	HalAlgSSF33_Init(Key, NULL); 
	HalAlgSSF33_Decrypt_Update(HALALGSSF33_MODE_ECB, pBuf, nSize, pBuf); 
	HalAlgSSF33_Final(); 

	return (0);
}

static UINT16 CommonAP_MsgSM1Decrypt(UINT8 *pBuf, UINT16 nSize, UINT8 Key[])
{
	HalAlgSM1_Init(Key, NULL); 
	HalAlgSM1_Decrypt_Update(HALALGSM1_MODE_ECB, pBuf, nSize, pBuf); 
	HalAlgSM1_Final(); 

	return (0);
}

static UINT16 CommonAP_MsgDESDecrypt(UINT8 *pBuf, UINT16 nSize, UINT8 Key[])
{
	HalAlgDES_Init(Key, CommonAP_AlgGetKeyLen(HALALGDES1_MODE_ECB), NULL); 
	HalAlgDES_Decrypt_Update(HALALGDES1_MODE_ECB, pBuf, nSize, pBuf); 
	HalAlgDES_Final();

	return (0);
}

static UINT32 CommonAP_MsgSM4Mac(UINT8 *pBuf, UINT16 nSize, UINT8 Init[], UINT8 Key[])
{
	UINT32 Mac;

	HalAlgSM4Mac_Init(Init, Key); 
	HalAlgSM4Mac_Update(pBuf, nSize); 
	HalAlgSM4Mac_Final((UINT8*)&Mac);

	return (Mac);
}

static UINT32 CommonAP_MsgSSF33Mac(UINT8 *pBuf, UINT16 nSize, UINT8 Init[], UINT8 Key[])
{
	UINT32 Mac;

	HalAlgSSF33Mac_Init(Init, Key); 
	HalAlgSSF33Mac_Update(pBuf, nSize); 
	HalAlgSSF33Mac_Final((UINT8*)&Mac);

	return (Mac);
}

static UINT32 CommonAP_MsgSM1Mac(UINT8 *pBuf, UINT16 nSize, UINT8 Init[], UINT8 Key[])
{
	UINT32 Mac;

	HalAlgSM1Mac_Init(Init, Key); 
	HalAlgSM1Mac_Update(pBuf, nSize); 
	HalAlgSM1Mac_Final((UINT8*)&Mac);	

	return (Mac);
}

static UINT16 CommonAP_MsgSha1(UINT8 *pBuf, UINT16 nSize, UINT8 *pHash)
{
	HalAlgSHA1_Init(); 
	HalAlgSHA1_Update(pBuf, nSize); 
	HalAlgSHA1_Final(pHash);

	return (0);
}

UINT16 CommonAP_MsgDecryptWithPadding(UINT8 *pIoBuf, UINT16 nSize, UINT8 Key[], UINT8 AlgType)
{
	UINT16 nRet = 0;

	switch(AlgType)
	{
		case COMM_ALGOTYPE_SM4:
			nRet = CommonAP_MsgSM4DecryptWithPadding(pIoBuf, nSize, Key);
			break;
		case COMM_ALGOTYPE_SSF33:
			nRet = CommonAP_MsgSSF33DecryptWithPadding(pIoBuf, nSize, Key);
			break;
		case COMM_ALGOTYPE_SM1:
			nRet = CommonAP_MsgSM1DecryptWithPadding(pIoBuf, nSize, Key);
			break;
		case COMM_ALGOTYPE_DES:
			nRet = CommonAP_MsgDESDecryptWithPadding(pIoBuf, nSize, Key);
			break;            
		default:
			break;
	}

	return (nRet);
}

void CommonAP_MsgDecrypt(UINT8 *pIoBuf, UINT16 nSize, UINT8 Key[], UINT8 AlgType)
{
	switch(AlgType)
	{
		case COMM_ALGOTYPE_SM4:
			CommonAP_MsgSM4Decrypt(pIoBuf, nSize, Key);
			break;
		case COMM_ALGOTYPE_SSF33:
			CommonAP_MsgSSF33Decrypt(pIoBuf, nSize, Key);
			break;
		case COMM_ALGOTYPE_SM1:
			CommonAP_MsgSM1Decrypt(pIoBuf, nSize, Key);
			break;
		case COMM_ALGOTYPE_DES:
			CommonAP_MsgDESDecrypt(pIoBuf, nSize, Key);
			break; 
		default:
			break;
	}

	return ;
}

UINT32 CommonAP_MsgMac(UINT8 *pInBuf, UINT16 nSize, UINT8 Init[], UINT8 Key[], UINT8 AlgType)
{
	UINT32 Mac = 0;

	switch(AlgType)
	{
		case COMM_ALGOTYPE_SM4:
			Mac = CommonAP_MsgSM4Mac(pInBuf, nSize, Init, Key);
			break;
		case COMM_ALGOTYPE_SSF33:
			Mac = CommonAP_MsgSSF33Mac(pInBuf, nSize, Init, Key);
			break;
		case COMM_ALGOTYPE_SM1:
			Mac = CommonAP_MsgSM1Mac(pInBuf, nSize, Init, Key);
			break;
		default:
			break;
	}

	return (Mac);
}

void CommonAP_MsgHash(UINT8 *pInBuf, UINT16 nSize, UINT8 *pHash, UINT8 AlgType)
{
	switch(AlgType)
	{
		case COMM_ALGOTYPE_SHA1:
			CommonAP_MsgSha1(pInBuf, nSize, pHash);
		default:
			break;
	}

	return ;
}
